<template>
  <div>
    <swiper :options="swiperOpiton" :key="keyId">
      <slot></slot>
      <div class="swiper-pagination" v-if="pagination" slot="pagination"></div>
    </swiper>
  </div>
</template>

<script>
export default {
  name: "MeSlider",
  data() {
    return {
      keyId: Math.random(),
      swiperOpiton: {}
    };
  },
  props: {
    direction: {
      type: String,
      defalut: "horizontal",
      validator(value) {
        return ["horizontal", "vertical"].indexOf(value) > -1;
      }
    },
    interval: {
      type: Number,
      defalut: 3000,
      validator(value) {
        return value >= 0;
      }
    },
    loop: {
      type: Boolean,
      defalut: true
    },
    pagination: {
      type: Boolean,
      defalut: true
    },
    data: {
      type: Array,
      defalut() {
        return [];
      }
    }
  },
  watch: {
    data(newData) {
      if (newData.length === 0) {
        return;
      }
      (this.swiperOpiton.loop = newData.length === 1 ? false : this.loop),
        (this.keyId = Math.random());
    }
  },
  created() {
    this.init();
  },
  methods: {
    init() {
      this.swiperOpiton = {
        watchOverflow: true,
        direction: this.direction,
        autoplay: this.interval
          ? {
              delay: this.interval,
              disableOnInteraction: false
            }
          : false,
        sliderPerView: 1,
        loop: this.data.length <= 1 ? false : this.loop,
        pagination: {
          el: this.pagination ? ".swiper-pagination" : null
        }
      };
    }
  }
};
</script>

<style lang="scss" scoped>
.swiper-container {
  width: 100%;
  height: 100%;
}
</style>
